/***************************************************************************
 *   Copyright (C) 2014 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include "vompremote.h"
#include "mvplistmodel.h"

MvpListModel::MvpListModel( VompRemote * mainWindow, QObject *parent ) : QAbstractListModel( parent ),
    m_mainWindow( mainWindow )
{
}


int MvpListModel::rowCount( const QModelIndex &/*parent*/ ) const
{
    return m_mainWindow->mvpList()->count();
}


int MvpListModel::columnCount( const QModelIndex &/*parent*/ ) const
{
    return MvpListModel::MvpColumnsLast;
}


QVariant MvpListModel::data( const QModelIndex &index, int role ) const
{
//    qDebug() << "MvpListModel::data role:" << role;
    if ( !index.isValid() )
        return QVariant();

    if ( index.row() >= m_mainWindow->mvpList()->size() )
        return QVariant();

    switch ( role )
    {
        case Qt::EditRole: //Requested from Live-TV epg through QDataWidgetMapper
        {
            if ( index.column() == MvpListModel::MvpName )
            {
                return m_mainWindow->mvpList()->at( index.row() ).name;
            }

            if ( index.column() == MvpListModel::MvpIp )
            {
                return m_mainWindow->mvpList()->at( index.row() ).ip.toString();
            }

            return QVariant();
        }

        case Qt::DisplayRole:
        case MvpListModel::MvpNameRole:
        {
            return m_mainWindow->mvpList()->at( index.row() ).name;
        }

        case MvpListModel::MvpIpRole:
        {
            return m_mainWindow->mvpList()->at( index.row() ).ip.toString();
        }

        case MvpListModel::MvpRowRole:
        {
            return index.row();
        }

        default:
        {
            return QVariant();
        }
    }
}


bool MvpListModel::insertRows( int position, int rows, const QModelIndex &/*parent*/ )
{
    if ( position > m_mainWindow->mvpList()->count() )
    {
        qDebug() << "MvpListModel::insertRows position out of range with:" << position << "  and count:" << m_mainWindow->channelList()->count() - 1;
        return false;
    }

    beginInsertRows( QModelIndex(), position, position + rows - 1 );

    for ( int row = 0; row < rows; ++row )
    {
        MvpEntry entry;
        m_mainWindow->mvpList()->insert( position, entry );
    }

    endInsertRows();

    return true;
}


bool MvpListModel::removeRows( int position, int rows, const QModelIndex &/*parent*/ )
{
    if ( rows <= 0 || rows + position > m_mainWindow->mvpList()->count() )
    {
        qDebug() << "MvpListModel::removeRows numbers out of range position:" << position
                 << "  rows:" << rows
                 << "  count:" << m_mainWindow->mvpList()->count();
        return false;
    }

    beginRemoveRows( QModelIndex(), position, position + rows - 1 );

    for ( int row = 0; row < rows; ++row )
    {
        m_mainWindow->mvpList()->removeAt( position );
    }

    endRemoveRows();

    return true;
}


QHash<int, QByteArray> MvpListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
        roles[MvpNameRole] = "MvpName";
        roles[MvpIpRole]   = "MvpIp";
        roles[MvpRowRole]  = "MvpRow";
    return roles;
}
